package jp.sourceforge.akjrcp.flvtools.explorer;



import java.util.List;

import org.eclipse.draw2d.AbstractHintLayout;
import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.widgets.Canvas;

public class FixLayout extends StackLayout{


	Canvas canvas;
	/**
	 * Returns the minimum size required by the input container. This is the size of the 
	 * largest child of the container, as all other children fit into this size.
	 * 
	 * @see AbstractHintLayout#calculateMinimumSize(IFigure, int, int)
	 */
	public FixLayout(Canvas canvas){
	this.canvas=canvas;
	}
	protected Dimension calculateMinimumSize(IFigure figure, int wHint, int hHint) {
		if (wHint > -1)
			wHint = Math.max(0, wHint - figure.getInsets().getWidth());
		if (hHint > -1)
			hHint = Math.max(0, hHint - figure.getInsets().getHeight());
		Dimension d = new Dimension();
		
		d.width=canvas.getClientArea().width;
		d.height=400;
		
		List children = figure.getChildren();
		IFigure child;
		int maxH=0;
		for (int i = 0; i < children.size(); i++) {
			child = (IFigure)children.get(i);
			int h=child.getBounds().y+child.getBounds().height;
			if(h>maxH){
				maxH=h;
			}
		}
		d.height=maxH;
		ExplorerView.log.info("children:"+children.size());
		ExplorerView.log.info("d:"+d);
		return d;

	}

	/**
	 * Calculates and returns the preferred size of the given figure.  This is the union of 
	 * the preferred sizes of the widest and the tallest of all its children. 
	 * 
	 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
	 */
	protected Dimension calculatePreferredSize(IFigure figure, int wHint, int hHint) {
		Dimension d = new Dimension();
		d.width=canvas.getClientArea().width;
		d.height=400;
		
		List children = figure.getChildren();
		IFigure child;
		int maxH=0;
		for (int i = 0; i < children.size(); i++) {
			child = (IFigure)children.get(i);
			int h=child.getBounds().y+child.getBounds().height;
			if(h>maxH){
				maxH=h;
			}
		}
		d.height=maxH;
		ExplorerView.log.info("children:"+children.size());
		ExplorerView.log.info("d:"+d);
		return d;
	}
	
	public void layout(IFigure figure) {
		ExplorerView.log.info("layout");
		List children = figure.getChildren();
		IFigure child;
		//int w=(int)(320*ExplorerView.root.getScale());
		//int h=(int)(240*ExplorerView.root.getScale());
		int w=320;
		int h=240;
		int nx=0;
		int ny=0;
		int mx=5;
		int my=5;
		int basew=canvas.getClientArea().width;
		for (int i = 0; i < children.size(); i++) {
			child = (IFigure)children.get(i);
			if(nx+w>basew){
				nx=0;
				ny+=h+my;
			}
			child.setBounds(new Rectangle(nx,ny,w,h));
			
			nx+=w+mx;
		}
	}

	
}
